import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class UserRepository {
    private static Map<String, User> users = new HashMap<>();
    private static Map<String, String> usernameToId = new HashMap<>();

    static {
        Admin admin = new Admin("admin001", "admin", "ynuinfo#777",
                LocalDateTime.now(), "13800138000");
        users.put(admin.getUserId(), admin);
        usernameToId.put(admin.getUsername(), admin.getUserId());
    }
    public static User getUserById(String userId) {
        return users.get(userId);
    }
    public static User getUserByUsername(String username) {
        String userId = usernameToId.get(username);
        return userId != null ? users.get(userId) : null;
    }
    public static boolean addUser(User user) {
        if (users.containsKey(user.getUserId()) || usernameToId.containsKey(user.getUsername())) {
            return false;
        }
        users.put(user.getUserId(), user);
        usernameToId.put(user.getUsername(), user.getUserId());
        Logger.log("增加" + user.getUserType() + " " + user.getUsername(), "system");
        return true;
    }
    public static boolean removeUser(String userId) {
        User user = users.get(userId);
        if (user != null && !user.getUserId().equals("admin001")) {
            users.remove(userId);
            usernameToId.remove(user.getUsername());
            Logger.log("删除" + user.getUserType() + " " + user.getUsername(), "system");
            return true;
        }
        return false;
    }
    public static List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }
}